let nearul = document.querySelector('.nearul');
let dataul = document.querySelector('.dataul');
let hotul = document.querySelector('.hotul');
let flaghot = true;
let flagnear = false;
// 最近受欢迎
async function getdata() {
    // console.log(1);
    // https://wx.maoyan.com/mmdb/movie/v1/list/wish/order/coming.json?ci=20&limit=30&offset=0
    let res = await pAjax({
        url: '/mao',
        // type:'post',
        data: {
            ci: 20,
            limit: 30,
            offset: 0,
        }

    });
    res = JSON.parse(res);
    // console.log(res.data.coming);
    Runder(res.data.coming);
}

function Runder(data) {
    // console.log(data);
    let str = '';
    data.forEach(item => {
        item.img = item.img.replace('/w.h', '');
        str += `
        <li idx="${item.id}">
        <img src="${item.img}" alt="">
        <p>${item.nm}</p>
        <i><b>${item.wish}</b>人想看</i>
        <br>
        <span>${item.rt}</span>
        <em class="iconfont icon-shoucang1
        "></em>
        </li>
        `;
    });
    nearul.innerHTML = str;
    jump();
    // console.log(1);
}

getdata();

// 待映的下面 最近上映
// let limit = 10;
async function getdataul() {
    let res = await pAjax({
        // https://wx.maoyan.com/mmdb/movie/v2/list/rt/order/coming.json?ci=20&limit=10 
        url: '/dai/movie/v2/list/rt/order/coming.json? ',
        data: {
            ci: 20,
            limit: 10,
        }
    });
    res = JSON.parse(res);
    // console.log(res.data.coming);
    Rundataul(res.data.coming);

}

function Rundataul(data) {
    // console.log(data);
    let str = '';
    data.forEach(item => {
        item.img = item.img.replace('/w.h', '');
        str += `
        <h5>${item.comingTitle}</h5>
    <li idx="${item.id}">
    <img src="${item.img}" alt="">
    <div class="dateMid">
        <h4>${item.nm}</h4>
        <p>${item.cat}</p>
        <p class='actor'>${item.desc}</p>
        <p><i>${item.pubDesc}</i></p>
    </div>
    <div class="dateRh">
        <p><i>${item.wish}</i>人想看</p>
        <span><i class="iconfont icon-shoucang1
            "></i>想看</span>
    </div>
    </li>
    `;
    });
    dataul.innerHTML += str;
    jump();
}
getdataul();


// 热映
let offset = 0;
async function gethotul() {

    let res = await pAjax({
        // https://wx.maoyan.com/mmdb/movie/v2/list/hot.json?limit=12&offset=0&ct=广州
        url: `/dai/movie/v2/list/hot.json?`,
        data: {
            limit: 12,
            offset: offset,
            ct: "广州",
        }
    });
    res = JSON.parse(res);
    console.log(res.data.hot);
    Runhotul(res.data.hot);
}

function Runhotul(data) {
    let str = '';
    let hotdate = new Date();
    data.forEach(item => {
        item.img = item.img.replace('/w.h', '');
        str += `
        <li idx="${item.id}">
        <img src="${item.img}" alt="">
          <div class="dateMid">
             <h4>${item.nm}</h4>
             <p>${item.cat}</p>
             <p class='actor'>${item.desc}</p>
        `;
        let sigledate = new Date(item.rt);
        if (sigledate - hotdate < 0) {
            str += `
            <p><i>${item.showInfo}</i></p>
            </div>
           <div class="dateRh">
          <p class='hotdateRh'>${item.sc}</i>分</p>
          <span style="background-color: red;">购票</span>
        </div>
        </li>
        `;

        } else {
            str += `
            <p><i>${item.showTimeInfo}</i></p>
            </div>
            <div class="dateRh">
          <p class='hotdateRh'><i>${item.wish}</i>人想看</p>
          <span>预售</span>
        </div>
        </li>
        `;
        }
    });
    hotul.innerHTML += str;
    let hotdateRh = document.querySelector('.hotdateRh');
    jump();


}
gethotul();


// 点击跳转详情页
async function jump() {
    let li = document.querySelectorAll('li');
    // console.log(li);
    li.forEach(item => {
        item.onclick = function () {
            let id = this.getAttribute('idx');
            console.log(id);
            location.href = '../html/details.html';
            setCookie('id', id);
        }
    });
    localStorage.setItem('url', location.href)

}


// 下拉刷新
$('main').scroll(function () {
    // console.log($(this)[0].scrollHeight);
    // console.log($('main').scrollTop());
    // console.log($('main').height());
    // offset++;
    if (flaghot) {
        if (Math.ceil($(this)[0].scrollHeight) - Math.ceil($('main').scrollTop()) - Math.ceil($('main').height()) == 0) {
            offset += 12;
            console.log(offset);
            gethotul();
            return;
        }
    }


    if (flagnear) {
        if (Math.ceil($(this)[0].scrollHeight) - Math.ceil($('main').scrollTop()) - Math.ceil($('main').height()) == 0) {
            // limit += 12;
            // console.log(limit);
            // getdataul();
            return;
        }
    }

    // pAjax({
    //         url: '/dai/movie/list/info.json?',
    //         data: {
    //             ci : 20,
    //             movieIds : '1211382%2C1346626%2C1383416%2C1278054%2C337487%2C1368406%2C93%2C18167%2C360%2C1298864 '}
    //         }).then(res => {
    //         res = JSON.parse(res);
    //         console.log(res);
    //     });

});




let hota = $('.hota');

// 城市列表显示
$('.navem').click(function () {
    $('.navul').slideToggle();
});


// 热映显示
hota.click(function () {
    $('.hot').show();
    $('.wait').hide();
    flaghot = true;
    flagnear = false;
    //gethotul();
});

// 待映显示
$('.waita').click(function () {
    $('.hot').hide();
    $('.wait').show();
    flaghot = false;
    flagnear = true;

});

$('.nava').click(function () {
    $('.nava a').removeClass('a1');
    // $(this.a).addClass('a1');
    // console.log(this.children);
    // $('this.childern').addClass('a1');
});